import { SpecificationClient as Client, GetListPageSpecificationInput as InputGetListModel, InsertOrUpdateSpecificationInput as InputInsertModel, OutputSpecificationSimpleModel as listItemModel, InsertOrUpdateSpecificationInput as DetailModel } from "@/api/api_specificationclient";
import { ApiExceptionShow } from "@/framework/utils/api-exception-show";
import notify from "devextreme/ui/notify";
import { Vue } from "vue-class-component";
/*
 * @Author: LAB
 * @Date: 2021-05-26 17:02:18
 * @Description: 药品规格
 * @FilePath: \client-app\src\data-source\drug-sku.ts
 */

export interface ListDataI<T> {
  data: T[],
  dataCount: number;
}
export default class DrugSkuData {
  client: Client;
  view: Vue
  constructor(client: Client, view: Vue) {
    this.client = client;
    this.view = view;
  }
  getList(body: InputGetListModel): Promise<ListDataI<listItemModel>> {
    return this.client.getListPageSpecification(body).then(res => {
      if (res.isSuccess) {
        console.log("res", res);
        return {
          data: res.listData,
          dataCount: res.pageInfo && res.pageInfo.count || 0
        }
      } else {
        notify(res.message, "error")
        return {
          data: [],
          dataCount: 0
        }
      }
    }, err => {
      ApiExceptionShow.showException(err, this.view)
      return {
        data: [],
        dataCount: 0
      }
    })
  }
  add(body: InputInsertModel) {
    return this.client.insertSpecification(body).then(res => {
      if (res.isSuccess) {
        notify(res.message, "success")
      } else {
        notify(res.message, "error")
      }
      return res.isSuccess
    }).catch(err => {
      notify(err.result.title, "error")
      return false
    })
  }
  update(body: InputInsertModel) {
    return this.client.updateSpecification(body).then (res => {
      if (res.isSuccess) {
        notify(res.message, "success")
      } else {
        notify(res.message, "error")
      }
      return res.isSuccess
    }).catch(err => {
      notify(err.result.title, "error")
      return false
    })
  }
  delete(body: string) {
    return this.client.deleteSpecification(body).then(res => {
      if (res.isSuccess) {
        notify("删除成功", "success")
      } else {
        notify(res.message, "error")
      }
      return res.isSuccess
    }).catch(err => {
      ApiExceptionShow.showException(err, this.view)
      return false
    })
  }
  info(id: string): Promise<DetailModel> {
    return this.client.getSpecificationInfo(id).then(res => {
      if (res.isSuccess) {
        return res.data
      } else {
        notify(res.message, "error")
        return {}
      }
    }, err => {
      ApiExceptionShow.showException(err, this.view)
      return {}
    })
  }
}